package com.gears_software.bbnetwork.application;

import android.app.Application;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.avos.avoscloud.AVAnalytics;
import com.avos.avoscloud.AVOSCloud;
import com.gears_software.bbnetwork.utlis.Image;
import com.gears_software.common.trinea.entitys.FailedReason;
import com.gears_software.common.trinea.services.impl.ImageCache.CompressListener;
import com.gears_software.common.trinea.services.impl.ImageMemoryCache.OnImageCallbackListener;
import com.gears_software.common.trinea.utils.FileUtils;
import com.gears_software.common.trinea.utils.ObjectUtils;

public class BBNetworkApplication extends Application {
	@Override
	public void onCreate() {
		super.onCreate();
		initImageCache();
		AVOSCloud.initialize(this, "KU4QORwr3LzDfdsi3vDU0sst", "CD4GE8y4fe1vPL5yIC60Oavf");
        //AVAnalytics.start(this);    已经不再需要这行代码了
        AVAnalytics.enableCrashReport(this, true);
	}
	
	void initImageCache() {
		
		Image.IMAGECACHE
				.initData(this.getApplicationContext(), Image.TAG_CACHE);
		Image.IMAGECACHE.setContext(this.getApplicationContext());
		Image.IMAGECACHE.setCacheFolder(Image.DEFAULT_CACHE_FOLDER);
		Image.IMAGECACHE.setCompressListener(new CompressListener() {

			@Override
			public int getCompressSize(String imagePath) {
				if (FileUtils.isFileExist(imagePath)) {
					long fileSize = FileUtils.getFileSize(imagePath) / 1000;
					/**
					 * if image bigger than 100k, compress to 1/(n + 1) width
					 * and 1/(n + 1) height, n is fileSize / 100k
					 **/
					if (fileSize > 100) {
						return (int) (fileSize / 100) + 1;
					}
				}
				return 1;
			}
		});
		Image.IMAGECACHE
				.setOnImageCallbackListener(new OnImageCallbackListener() {

					@Override
					public void onPreGet(String imageUrl, View view) {
						// TODO Auto-generated method stub
					}

					@Override
					public void onGetSuccess(String imageUrl,
							Bitmap loadedImage, View view, boolean isInCache) {
						if (view.getTag() != null) {
							Log.d("SFRS ", "-------------------------start");
							Log.d("SFRS", imageUrl);
							Log.d("ISFRSM", view.getTag().toString());
							Log.d("SFRS", "-------------------------end");
						}
						if (view != null && loadedImage != null) {
							ImageView imageView = (ImageView) view;
							// add tag judge, avoid listView cache and so on
							String imageUrlTag = (String) imageView.getTag();
							if (ObjectUtils.isEquals(imageUrlTag, imageUrl)) {
								imageView.setImageBitmap(loadedImage);
							}
						}
					}
					@Override
					public void onGetNotInCache(String imageUrl, View view) {
						// TODO Auto-generated method stub
					}
					@Override
					public void onGetFailed(String imageUrl,
							Bitmap loadedImage, View view,
							FailedReason failedReason) {
						// TODO Auto-generated method stub
					}
				});
	}
	
	// 杀进程
	public void onLowMemory() {
		super.onLowMemory();
		System.gc();
	}
}
